package com.education.business.service.education;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.education.business.mapper.education.SubjectInfoMapper;
import com.education.business.service.BaseService;
import com.education.common.model.PageInfo;
import com.education.common.utils.FileUtil;
import com.education.model.dto.SubjectInfoDto;
import com.education.model.entity.SubjectInfo;
import com.education.model.request.PageParam;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author zengjintao
 * @version 1.0
 * @create_at 2020/11/19 13:53
 */
@Service
public class SubjectInfoService extends BaseService<SubjectInfoMapper, SubjectInfo> {

    public PageInfo<SubjectInfoDto> selectPageList(PageParam pageParam, SubjectInfo subjectInfo) {
        Page<SubjectInfoDto> page = new Page<>(pageParam.getPageNumber(), pageParam.getPageSize());
        return selectPage(baseMapper.selectPageList(page, subjectInfo));
    }
    /**
     * 根据考试任务查询
     * 读取并写入json文件
     * @param testPaperArray
     * @return
     */
    public SubjectInfo selectByTestPaperId(JSONArray testPaperArray){
        List<SubjectInfo> subjectInfoArrayList = new ArrayList<>();
        SubjectInfo subjectInfo = new SubjectInfo();
        for(int i=0;i<testPaperArray.size();i++){
            JSONObject testPaper = JSONObject.parseObject(testPaperArray.getString(i));
            subjectInfo = baseMapper.selectByTestPaperId(testPaper.getInteger("id"));
            subjectInfoArrayList.add(subjectInfo);
        }
        FileUtil.WriteFile("subjectInfo"+".json",FileUtil.tojson(subjectInfoArrayList));
        return subjectInfo;
    }
    public int saveAll(SubjectInfo subjectInfo){
        return baseMapper.saveAll(subjectInfo);
    }

}
